# import pygame
# import sys, random, math
#
# pygame.init()           # 初始化pygame
# pygame.font.init()     # 确保中文能正常显示
# font = pygame.font.SysFont(["SimHei", "WenQuanYi Micro Hei", "Heiti TC"], 36)
# # 游戏常量
# WIDTH, HEIGHT = 800, 600
# PADDLE_WIDTH, PADDLE_HEIGHT = 15, 100
# BALL_RADIUS = 10
# PADDLE_SPEED = 5
# BALL_SPEED = 5
# WHITE = (255, 255, 255)
# BLACK = (0, 0, 0)
# RED = (255, 0, 0)
# # 创建游戏窗口
# screen = pygame.display.set_mode((WIDTH, HEIGHT))
# pygame.display.set_caption("乒乓球游戏")
# # 加载音效
# pygame.mixer.init()
# hit_sound = pygame.mixer.Sound(pygame.mixer.Sound(pygame.mixer.Sound("data/hit.wav")))
# score_sound = pygame.mixer.Sound(pygame.mixer.Sound("data/score.mp3"))
# class Paddle:
#     def __init__(self, x, y):
#         self.rect = pygame.Rect(x, y, PADDLE_WIDTH, PADDLE_HEIGHT)
#         self.score = 0
#     def draw(self):
#         pygame.draw.rect(screen, WHITE, self.rect)
#     def move_up(self):
#         if self.rect.top > 0:
#             self.rect.y -= PADDLE_SPEED
#     def move_down(self):
#         if self.rect.bottom < HEIGHT:
#             self.rect.y += PADDLE_SPEED
#     def reset(self):
#         self.rect.y = HEIGHT // 2 - PADDLE_HEIGHT // 2
#
# class Ball:
#     def __init__(self, x, y):
#         self.reset(x, y)
#     def draw(self):
#         pygame.draw.circle(screen, WHITE, (self.x, self.y), BALL_RADIUS)
#     def move(self):
#         self.x += self.dx * BALL_SPEED
#         self.y += self.dy * BALL_SPEED
#         # 碰撞顶部或底部
#         if self.y - BALL_RADIUS <= 0 or self.y + BALL_RADIUS >= HEIGHT:
#             self.dy *= -1
#             hit_sound.play()
#         return self.x - BALL_RADIUS <= 0 or self.x + BALL_RADIUS >= WIDTH
#     def reset(self, x, y):
#         self.x, self.y  = x, y
#         angle = random.uniform(-math.pi / 4, math.pi / 4)
#         direction = 1 if random.random() < 0.5 else -1
#         self.dx = direction * math.cos(angle)
#         self.dy = math.sin(angle)
#
# def main():
#     clock = pygame.time.Clock()
#     player = Paddle(20, HEIGHT // 2 - PADDLE_HEIGHT // 2)
#     opponent = Paddle(WIDTH - 20 - PADDLE_WIDTH, HEIGHT // 2 - PADDLE_HEIGHT // 2)
#     ball = Ball(WIDTH // 2, HEIGHT // 2)
#     # 游戏主循环
#     running = True
#     while running:
#         screen.fill(BLACK)
#         # 事件处理
#         for event in pygame.event.get():
#             if event.type == pygame.QUIT:
#                 running = False
#         # 玩家控制
#         keys = pygame.key.get_pressed()
#         if keys[pygame.K_UP]:
#             player.move_up()
#         if keys[pygame.K_DOWN]:
#             player.move_down()
#         # AI控制对手
#         if ball.dx > 0:  # 球向对手移动
#             if ball.y > opponent.rect.centery + 10:
#                 opponent.move_down()
#             elif ball.y < opponent.rect.centery - 10:
#                 opponent.move_up()
#         # 球的移动
#         scored = ball.move()
#         # 碰撞检测
#         if (ball.x - BALL_RADIUS <= player.rect.right and
#                 ball.y >= player.rect.top and
#                 ball.y <= player.rect.bottom and
#                 ball.dx < 0):
#             ball.dx *= -1
#             hit_sound.play()
#         if (ball.x + BALL_RADIUS >= opponent.rect.left and
#                 ball.y >= opponent.rect.top and
#                 ball.y <= opponent.rect.bottom and
#                 ball.dx > 0):
#             ball.dx *= -1
#             hit_sound.play()
#         # 计分
#         if scored:
#             score_sound.play()
#             if ball.x < WIDTH // 2:
#                 opponent.score += 1
#             else:
#                 player.score += 1
#             ball.reset(WIDTH // 2, HEIGHT // 2)
#         # 绘制游戏元素
#         player.draw()
#         opponent.draw()
#         ball.draw()
#         # 绘制计分板
#         score_text = f"玩家: {player.score}  AI: {opponent.score}"
#         text_surface = font.render(score_text, True, WHITE)
#         screen.blit(text_surface, (WIDTH // 2 - text_surface.get_width() // 2, 20))
#         # 绘制中间线
#         pygame.draw.line(screen, WHITE, (WIDTH // 2, 0), (WIDTH // 2, HEIGHT), 2)
#         pygame.display.flip()
#         clock.tick(60)
#     pygame.quit()
#     sys.exit()
#
# if __name__ == "__main__":
#     main()


